﻿using System;
using System.Data;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
using KPIS.GERP.GHRMS.BLL;
using KPIS.GERP.GHRMS.IBLL;
using KPIS.GERP.GHRMS.MODEL;
using _WebShared.Classes;

namespace KPIS.GERP.GHRMS.APP.App_UI.Forms
{
    public partial class PS_RoyalItemHistories : System.Web.UI.Page
    {
        private SsMng ssMng = new SsMng();
        private IRoyalItemHistoriesBLL hisBLL = new RoyalItemHistoriesBLL();
        private IRoyalItemRequestBLL reqBLL = new RoyalItemRequestBLL();
        private ITreeViewBLL treeBLL = new TreeViewBLL();

        private RoyalItemRequestInfo reqInfo = new RoyalItemRequestInfo();
        private RadComboBox cmbYear, cmbEmployeeType;
        protected void Page_Load(object sender, EventArgs e)
        {
            cmbYear = (RadComboBox)cbYear.FindControl("cbYear");
            cmbEmployeeType = (RadComboBox)cbEmployeeType.FindControl("cbEmployeeType");

            if (!Page.IsPostBack)
            {
                cbEmployeeType.ListEmployeeTypeComboBox();
                cbYear.ListYear();
                cmbYear.SelectedValue = Convert.ToString(Int32.Parse(DateTime.Today.Year.ToString()) + 543);

                LoadRootNodes(RadTreeView1, TreeNodeExpandMode.ServerSideCallBack);
                RadTreeNode selected_node = RadTreeView1.SelectedNode;

                LoadDataGrid();
            }
        }
        public void LoadDataGrid()
        {
            string Year = cmbYear.SelectedValue;

            IList<RoyalItemHistoryInfo> Info = hisBLL.ListRoyalItemHistoriesByYear(Year);

            RadGrid1.DataSource = Info;
            RadGrid1.DataBind();
        }
        private void LoadRootNodes(RadTreeView treeView, TreeNodeExpandMode expandMode)
        {
            string EmpType = cmbEmployeeType.SelectedValue;
            string Year = cmbYear.SelectedValue;

            IList<TreeViewInfo> infoList = treeBLL.TreeRoyalItemRequest(EmpType, Year, "");

            foreach (TreeViewInfo info in treeBLL.TreeRoyalItemRequest(EmpType, Year, ""))
            {
                RadTreeNode node = new RadTreeNode();
                node.Text = info.Name;
               // node.ToolTip = info.Descr;
                node.Value = info.Id;
                node.ImageUrl = info.Image_url;
                if (Convert.ToInt32(info.Children_count) > 0)
                {
                    node.ExpandMode = expandMode;
                }
                treeView.Nodes.Add(node);
            }
        }

        public void PopulateNodeOnDemand(RadTreeNodeEventArgs e, TreeNodeExpandMode expandMode)
        {
            string EmpType = cmbEmployeeType.SelectedValue;
            string Year = cmbYear.SelectedValue;

            e.Node.Dispose();
            e.Node.Nodes.Clear();

            foreach (TreeViewInfo info in treeBLL.TreeRoyalItemRequest(EmpType, Year, e.Node.Value))
            {
                RadTreeNode node = new RadTreeNode();
                node.Text = info.Name;
               // node.ToolTip = info.Descr;
                node.Value = info.Id;
                node.ImageUrl = info.Image_url;
                if (Convert.ToInt32(info.Children_count) > 0)
                {
                    node.ExpandMode = expandMode;
                }
                e.Node.Nodes.Add(node);
            }

            e.Node.Expanded = true;
            e.Node.ExpandMode = expandMode;
        }

        public void PopulateNodeOnEditGrid(RadTreeNodeEventArgs e, TreeNodeExpandMode expandMode)
        {
            string EmpType = cmbEmployeeType.SelectedValue;
            string Year = cmbYear.SelectedValue;

            foreach (TreeViewInfo info in treeBLL.TreeRoyalItemRequest(EmpType, Year, e.Node.Value))
            {
                RadTreeNode node = new RadTreeNode();
                node.Text = info.Name;
                //node.ToolTip = info.Descr;
                node.Value = info.Id;
                node.ImageUrl = info.Image_url;
                if (Convert.ToInt32(info.Children_count) > 0)
                {
                    node.ExpandMode = expandMode;
                }
                e.Node.Nodes.Add(node);
            }

            e.Node.Nodes.Clear();
            e.Node.ExpandMode = expandMode;
        }
        public void RefreshTree()
        {
            RadTreeNode selected_node = RadTreeView1.SelectedNode;
            if (selected_node == null)
            {
                selected_node = RadTreeView1.FindNodeByValue("0");
            }
            RadTreeNodeEventArgs node = new RadTreeNodeEventArgs(selected_node);
            RadAjaxManager1.ResponseScripts.Add(@"CollapseSelectTree();");
            PopulateNodeOnEditGrid(node, TreeNodeExpandMode.ServerSideCallBack);
            RadAjaxManager1.ResponseScripts.Add(@"ExpandSelectTree();");
        }
        protected void btnExpand_Click(object sender, ImageClickEventArgs e)
        {
            string EmpType = cmbEmployeeType.SelectedValue;
            string Year = cmbYear.SelectedValue;

            IList<TreeViewInfo> infoList = treeBLL.TreeRoyalItemRequest(EmpType, Year, "");

            RadTreeView1.Nodes.Clear();
            RadTreeView1.DataSource = infoList;
            RadTreeView1.DataBind();
            RadTreeView1.ExpandAllNodes();
        }
        protected void btnCollapse_Click(object sender, EventArgs e)
        {
            RadTreeView1.CollapseAllNodes();
        }
        protected void cmdExpand_Click(object sender, ImageClickEventArgs e)
        {
            string EmpType = cmbEmployeeType.SelectedValue;
            string Year = cmbYear.SelectedValue;

            IList<TreeViewInfo> infoList = treeBLL.TreeRoyalItemRequest(EmpType, Year, "");

            RadTreeView1.Nodes.Clear();
            RadTreeView1.DataSource = infoList;
            RadTreeView1.DataBind();
            RadTreeView1.ExpandAllNodes();
        }

        protected void cmdCollapse_Click(object sender, ImageClickEventArgs e)
        {
            RadTreeView1.CollapseAllNodes();
        }

        protected void RadTreeView1_NodeExpand(object sender, RadTreeNodeEventArgs e)
        {
            PopulateNodeOnDemand(e, TreeNodeExpandMode.ServerSideCallBack);

        }
       
        protected void RadGrid1_ItemCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
        {
            if (e.CommandName == "Delete")
            {
                int seq = Convert.ToInt32((e.Item as GridDataItem).OwnerTableView.DataKeyValues[e.Item.ItemIndex]["Sequence"]);

                hisBLL.Delete(seq, ssMng.EmpSeqI);
            }
            
            RefreshTree();
            LoadDataGrid();
        }

        protected void cmdAutoAddNew_Click(object sender, EventArgs e)
        {
            Session["dsSession"] = "";

            IList<RadTreeNode> nodeCollection = RadTreeView1.CheckedNodes;
            List<RoyalItemHistoryInfo> nodeList = new List<RoyalItemHistoryInfo>();
            RoyalItemHistoryInfo info = new RoyalItemHistoryInfo();
            foreach (RadTreeNode node in nodeCollection)
            {
                if (node.Level == 2)
                {
                    info.Sequence = Int32.Parse(node.Value);
                    nodeList.Add(info);
                }
            }

            if (nodeList.Count > 0)
            {
                Session["dsSession"] = nodeList;
                RadAjaxManager1.ResponseScripts.Add(@"openRadWin();");
            }
            else
            {
                MessageX.Alert(this, "กรุณาเลือกรายการ");
                return;
            }
        }

        protected void cmdFilter_Click(object sender, EventArgs e)
        {
            RadTreeView1.Nodes.Clear();

            LoadRootNodes(RadTreeView1, TreeNodeExpandMode.ServerSideCallBack);
            RadTreeNode selected_node = RadTreeView1.SelectedNode;

            LoadDataGrid();
        }

    }
}
